﻿$ssa = Get-SPEnterpriseSearchServiceApplication | Where-Object {$_.Name -eq "Search Service Application"} 
$ssaContent = new-object Microsoft.Office.Server.Search.Administration.Content($ssa) 
 
$logViewer = New-Object Microsoft.Office.Server.Search.Administration.Logviewer $ssa 
$ErrorList = $logViewer.GetAllStatusMessages() | Select ErrorId 
 
Foreach ($errorId in $ErrorList) 
{ 
    if ($errorId.errorId -eq 0 -or $errorId.errorId -eq 1 -or $errorId.errorId -eq 2 -or $errorId.errorId -eq 745) 
    { 
 
    } 
    else 
    { 
        $crawlLogFilters = New-Object Microsoft.Office.Server.Search.Administration.CrawlLogFilters; 
        $crawlLogFilters.AddFilter("MessageId", $errorId.errorId); 
        #$crawlLogFilters.AddFilter("Url", "Contains", "\\officeshare\user"); 
        #$crawlLogFilters.AddFilter("ContentSourceId", 86); 
        "Processing Error Code: " + $errorId.errorId; 
        $startNum = 0; 
        $errorItems += $logViewer.GetCurrentCrawlLogData($crawlLogFilters, ([ref] $startNum)); 
        Write-Host "Processing $startNum"; 
        WHILE($startNum -ne -1) 
        { 
            $crawlLogFilters.AddFilter("StartAt", $startNum); 
            $startNum = 0; 
            $errorItems += $logViewer.GetCurrentCrawlLogData($crawlLogFilters, ([ref] $startNum)); 
            Write-Host "Processing $startNum"; 
        } 
    } 
} 
$errorItems | Export-CSV crawllog.csv 
